package org.ling.xuan.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.common.CommonBizUtil;
import org.ling.xuan.condition.LxCondition;
import org.ling.xuan.dao.LocalAddDao;
import org.ling.xuan.dao.MarkFileDao;
import org.ling.xuan.dialog.AddFileListDialog;
import org.ling.xuan.dialog.AddInfoDialog;
import org.ling.xuan.entity.mo.LocalAdd;
import org.ling.xuan.entity.mo.MarkFile;
import org.ling.xuan.util.LoggerUtil;

import java.util.List;

/**
 * 添加提交
 *
 * @author liting50684
 * @date 2023/08/01
 */
public class LxLocalAddAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        List<MarkFile> files = MarkFileDao.listUnAddFiles();
        AddFileListDialog fileDialog = new AddFileListDialog(e.getProject(), true, files,true);
        showFileDialog(fileDialog, e);
    }

    private void showFileDialog(AddFileListDialog fileDialog, AnActionEvent e) {
        if (fileDialog.showAndGet()) {
            List<String> markFileIds = fileDialog.getSelectIds();
            List<String> modifyFiles = fileDialog.getSelectFiles(false);
            if (modifyFiles.size() > 0) {
                fileDialog.consoleSelectedFileInfo(e.getProject());
                LocalAdd add = LocalAddDao.getCur();
                String od = add.getId();
                AddInfoDialog addInfoDialog = new AddInfoDialog(e.getProject(), true, add);
                showAddInfoDialog(e, markFileIds, modifyFiles, od, addInfoDialog);
            } else {
                Messages.showMessageDialog(
                        e.getProject(),
                        "未选中任何修改文件",
                        "提示",
                        Messages.getWarningIcon()
                );
            }
        }
    }

    private void showAddInfoDialog(AnActionEvent e, List<String> markFileIds, List<String> modifyFiles, String od, AddInfoDialog addInfoDialog) {
        LocalAdd add;
        if (addInfoDialog.showAndGet()) {
            add = addInfoDialog.getSetting();
            boolean skipSql = add.isSkipSql();
            add.setId(od);
            LocalAddDao.updateCurAddSimple(add);
            add.setMarkFileIds(markFileIds);
            LocalAdd checkAddInfo =LocalAddDao.getCur();
            checkAddInfo.setSkipSql(skipSql);
            int i = CommonBizUtil.checkFiles(e.getProject(), modifyFiles,checkAddInfo);
            boolean next = true;
            if (i != 1) {
                next = false;
                String msg = "";
                if (i == 0) {
                    msg = "检测不通过,是否继续";
                } else {
                    msg = "存在告警,是否继续";
                }
                int result = Messages.showOkCancelDialog(e.getProject(), msg, "提示", "继续", "放弃", Messages.getWarningIcon());
                if (result == 0) {
                    next = true;
                }
            }
            if (next) {
                LocalAddDao.updateWithFile(add);
                CommonBizUtil.add(e.getProject(),od);
                LoggerUtil.log(e.getProject(), "添加成功...");
                Messages.showMessageDialog(
                        e.getProject(),
                        "添加成功",
                        "提示",
                        Messages.getInformationIcon()
                );
            }
        }
    }


    @Override
    public void update(AnActionEvent e) {
        boolean enable = LxCondition.getInstance().addCondition(e);
        e.getPresentation().setEnabledAndVisible(enable);
    }
}
